//
//  AUAddressPickerInnerVC.swift
//  CreditLoans
//
//  Created by 谢强 on 2017/12/8.
//  Copyright © 2017年 com.ucfpay. All rights reserved.
//

import UIKit

import AwesomeUBaseModule
import SnapKit

class AUAddressPickerInnerVC: UIViewController, AUAddressPickerProtocol {
    
    var dataArray: [AUAddressPickerModel]?
    
    fileprivate var selectedIndexPath: IndexPath?
    
    fileprivate lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.register(AUAddressPickerInnerCell.self, forCellReuseIdentifier: "cell")
        tableView.backgroundColor = UIColor.white
        tableView.separatorStyle = .none
        tableView.showsHorizontalScrollIndicator = false
        tableView.dataSource = self
        tableView.delegate = self
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        } else {
            automaticallyAdjustsScrollViewInsets = false;
        }
        
        view.addSubview(tableView)
        
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
}

extension AUAddressPickerInnerVC: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray?.count ?? 0
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AUAddressPickerInnerCell
        cell.model = dataArray?[indexPath.row]
        return cell
    }
}

extension AUAddressPickerInnerVC: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
        if tableView.indexPathForSelectedRow == indexPath {return nil}
        if selectedIndexPath != nil {
            dataArray?[selectedIndexPath!.row].selected = false
            tableView.reloadRows(at: [selectedIndexPath!], with: .none)
        }
        return indexPath
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        guard let name = dataArray?[indexPath.row].name else {
            return
        }
        
        selectedIndexPath = indexPath
        dataArray?[indexPath.row].selected = true
        tableView.reloadRows(at: [indexPath], with: .none)
        selectedCallback?(name, dataArray?[indexPath.row]._child)
    }
}

class AUAddressPickerInnerCell: UITableViewCell {
    
    var model: AUAddressPickerModel? {
        didSet {
            guard let model = model else {
                return
            }
            
            addressLabel.text = model.name
            
            if model.selected {
                // 选中
                addressLabel.textColor = UIColor(au_hexString: "#379FFB")
                selectionImageView.isHidden = false
            } else {
                // 普通
                addressLabel.textColor = UIColor(au_hexString: "#2B2C2E")
                selectionImageView.isHidden = true
            }
        }
    }
    
    let addressLabel = UILabel()
    
    private lazy var selectionImageView: UIImageView = {
        let bundle = Bundle.init(for: type(of: self))
        if let bundleDict = bundle.infoDictionary {
            let bundleName = bundleDict["CFBundleName"] ?? ""
            if let imagePath = bundle.path(forResource: "addressPicker_selected@2x", ofType: "png", inDirectory: "\(bundleName).bundle") {
                if let image = UIImage(contentsOfFile: imagePath) {
                    return UIImageView(image: image)
                }
            }
        }
        return UIImageView()
    }()
    
//    private let selectionImageView = UIImageView(image: #imageLiteral(resourceName: "对勾"))
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        addressLabel.textAlignment = .left
        addressLabel.textColor = UIColor(au_hexString: "#2B2C2E")
        addressLabel.font = UIFont.au_scaledFont(size: 14)
        
        contentView.addSubview(addressLabel)
        contentView.addSubview(selectionImageView)
        
        addressLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(15 * au_kScreenScale)
        }
        selectionImageView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(addressLabel.snp.right).offset(20 * au_kScreenScale)
        }
        
        selectionImageView.isHidden = true
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
